/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.view.renderer
{
	import multimedia.control.event.feed.UpdateFeedEvent;
	import multimedia.model.vo.FeedVO;
	
	import mx.controls.ComboBox;
	import mx.events.FlexEvent;
	import mx.events.ListEvent;

	public class ValidTimeComboBoxRenderer extends ComboBox
	{

		private static const DAY_IN_SECONDS:uint = 24 * 60 * 60;
		private static const WEEK_IN_SECONDS:uint = 7 * DAY_IN_SECONDS;
		private static const MONTH_IN_SECONDS:uint = 4 * WEEK_IN_SECONDS;

		[Bindable]
		private var validTimes:Array = [{label: "1 Tag", data: DAY_IN_SECONDS},
										{label: "2 Tage", data: DAY_IN_SECONDS * 2},
										{label: "3 Tage", data: DAY_IN_SECONDS * 3},
										{label: "4 Tage", data: DAY_IN_SECONDS * 4},
										{label: "5 Tage", data: DAY_IN_SECONDS * 5},
										{label: "6 Tage", data: DAY_IN_SECONDS * 6},
										{label: "1 Woche", data: WEEK_IN_SECONDS},
										{label: "2 Wochen", data: WEEK_IN_SECONDS * 2},
										{label: "3 Wochen", data: WEEK_IN_SECONDS * 3},
										{label: "1 Monat", data: MONTH_IN_SECONDS}]

		public function ValidTimeComboBoxRenderer()
		{
			super();
			dataProvider = validTimes;
			
			addEventListener(FlexEvent.DATA_CHANGE, onDataChange);
			addEventListener(ListEvent.CHANGE, onSelectionChange);
		}
		
		private function onDataChange(event:FlexEvent):void
		{
			if(super.data)
			{
				for(var i:uint = 0; i < validTimes.length; i++)
				{
					if((super.data as FeedVO).feed_item_valid_time == validTimes[i].data)
					{
						selectedIndex = i;
					}
				}
			}
		}
		
		private function onSelectionChange(event:ListEvent):void
		{
			var feed:FeedVO = data as FeedVO;
			feed.feed_item_valid_time = selectedItem.data;
			new UpdateFeedEvent(feed).dispatch();
		}
	}
}